package com.example.demo.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "分页结果")
public class PageResult<T> {

    @Schema(description = "当前页数据")
    @Builder.Default
    private List<T> records = Collections.emptyList();

    @Schema(description = "总记录数", example = "100")
    private long total;

    @Schema(description = "当前页码", example = "1")
    private long current;

    @Schema(description = "每页数量", example = "10")
    private long pageSize;

    public <R> PageResult<R> map(Function<T, R> mapper) {
        List<R> mapped = records.stream().map(mapper).collect(Collectors.toList());
        return PageResult.<R>builder()
                .records(mapped)
                .total(total)
                .current(current)
                .pageSize(pageSize)
                .build();
    }
}
